//
//  NSString+Trans.m
//  MovingLoaniOS
//
//  Created by RAIN on 2018/9/11.
//  Copyright © 2018 - 2019 GWMFC. All rights reserved.
//

#import "NSString+Trans.h"

@implementation NSString (Trans)

+ (NSString *)transFrom:(NSString *)chinese {
    return [[self class] transFrom:chinese hasPhonogram:NO uppercase:YES];
}

+ (NSString *)transFrom:(NSString *)chinese
           hasPhonogram:(BOOL)has
              uppercase:(BOOL)uppercase
{
    NSMutableString *pinyin = [chinese mutableCopy];
    if (pinyin && ![pinyin isEqualToString:@""]) {
        CFStringTransform((__bridge CFMutableStringRef)pinyin, NULL, kCFStringTransformMandarinLatin, NO);
        NSLog(@"pinyin_1: %@", pinyin);

        if (!has) {
            CFStringTransform((__bridge CFMutableStringRef)pinyin, NULL, kCFStringTransformStripCombiningMarks, NO);
            NSLog(@"pinyin_2: %@", pinyin);
        }

        if (uppercase) {
            pinyin = (NSMutableString *)pinyin.uppercaseString;
        }
    }
    return pinyin;
}

+ (NSString *)rg_allowDotFrom:(NSString *)string {
    NSString *allowString = [string stringByReplacingOccurrencesOfString:@"•" withString:@"·"];
    allowString = [allowString stringByReplacingOccurrencesOfString:@"▪" withString:@"·"];
    return allowString;
}

+ (NSString *)dateStringFromIDNumber:(NSString *)number {
    NSString *year = [number substringWithRange:NSMakeRange(6, 4)];
    NSString *month = [number substringWithRange:NSMakeRange(10, 2)];
    NSString *day = [number substringWithRange:NSMakeRange(12, 2)];
    NSString *date = [NSString stringWithFormat:@"%@-%@-%@", year, month, day];
    return date;
}

+ (NSString *)genderStringFromIDNumber:(NSString *)number {
    NSInteger genderTag = [[number substringWithRange:NSMakeRange(16, 1)] integerValue];
    NSString *gender = (genderTag % 2 == 0) ? @"女" : @"男";
    return gender;
}

+ (NSString *)rg_stringOfAppVersion {

    NSString * infoPath = [[NSBundle mainBundle] pathForResource:@"Info" ofType:@"plist"];
    NSDictionary * infoDic = [NSDictionary dictionaryWithContentsOfFile:infoPath];
    NSString * version = infoDic[@"CFBundleShortVersionString"];
    NSString * build = infoDic[@"CFBundleVersion"];

    return [NSString stringWithFormat:@"%@.%@", version, build];
}

+ (NSString *)rg_stringOfDisplayName {
    NSString * infoPath = [[NSBundle mainBundle] pathForResource:@"Info" ofType:@"plist"];
    NSDictionary * infoDic = [NSDictionary dictionaryWithContentsOfFile:infoPath];
    NSString * displayName = infoDic[@"CFBundleDisplayName"];

    return displayName;
}

- (BOOL)rg_containsAnyStringsIn:(NSArray *)strings {
    BOOL isContain = NO;
    for (NSString *string in strings) {
        isContain = isContain || [self containsString:string];
        if (isContain == YES) {
            return isContain;
        }
    }
    return isContain;
}

// 身份证号
- (BOOL)rg_validateIdentityCard {
    BOOL flag;
    if (self.length <= 0) {
        flag = NO;
        return flag;
    }
    NSString *identityCardRegex = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
    return [self rg_validateWithRegexString:identityCardRegex];
}

// 手机号码验证
- (BOOL)rg_validateMobile {
    /*
     号码号段
     1. 中国移动号段：134、135、136、137、138、139、150、151、152、157、158、159、147、182、183、184、187、188、1705、178
     2. 中国联通号段：130、131、132、145（145属于联通无线上网卡号段）、155、156、185、186 、176、1709、171[1]
     3. 中国电信号段：133 、153 、180 、181 、189、1700、177、173
     4. 与联通合作的虚拟运营商号段：1709、1708、1707、1718、1719开头；另：1713、1715、1716、 1717开头新号段正在配置中，暂未开通。
     */
    /*
    NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9])|(17[3,6,7,8]))\\d{8}$|^((170[0,5,7-9])|(171[8,9]))\\d{7}$";
    */
    NSString *phoneRegex = @"^\\d{11}$";
    return [self rg_validateWithRegexString:phoneRegex];
}

- (BOOL)rg_validateCompanyPhone {
    NSString *companyPhoneRegex = @"^\\d{3,25}|(\\d{3}-)\\d{3,22}|^(\\d{4}-)\\d{3,21}$";
    return [self rg_validateWithRegexString:companyPhoneRegex];
}

- (BOOL)rg_validateDate {
    NSString *dateRegex = @"^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$";
    return [self rg_validateWithRegexString:dateRegex];
}

// 密码验证
- (BOOL)rg_validatePassword {
    NSString *pwdRegex = @"[A-Za-z0-9]*$";
    return [self rg_validateWithRegexString:pwdRegex];
}

- (BOOL)rg_validateWithRegexString:(NSString *)regexString {
    NSPredicate *regex = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regexString];
    return [regex evaluateWithObject:self];
}

- (NSMutableAttributedString *)rg_attributedStringWithKeyword:(NSString *)keyword
                                                 keywordColor:(UIColor *)color
{
    return [self rg_attributedStringWithKeyword:keyword keywordColor:color keywordFont:nil];
}

- (NSMutableAttributedString *)rg_attributedStringWithKeyword:(NSString *)keyword
                                                 keywordColor:(UIColor *)color
                                                  keywordFont:(UIFont *)font
{
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:self];
    if (keyword && ![keyword isEqualToString:@""]) {
        NSRange keywordRange = [self rangeOfString:keyword];
        if (color) {
            [attrString addAttribute:NSForegroundColorAttributeName value:color range:keywordRange];
        } else {
            UIColor *defaultColor = [UIColor blackColor];
            [attrString addAttribute:NSForegroundColorAttributeName value:defaultColor range:keywordRange];
        }

        if (font) {
            [attrString addAttribute:NSFontAttributeName value:font range:keywordRange];
        } else {
            UIFont *defaultFont = [UIFont fontWithName:@"PingFang SC" size:12.0];
            [attrString addAttribute:NSFontAttributeName value:defaultFont range:keywordRange];
        }
    }
    return attrString;
}

- (id)rg_toObject {
    NSString *string = [self stringByReplacingOccurrencesOfString:@"\0" withString:@""];
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error;
    id obj = [NSJSONSerialization JSONObjectWithData:data
                                             options:NSJSONReadingMutableContainers
                                               error:&error];
    NSLog(@"RGAppUtils NSString to object Error:\n%@", error);
    return obj;
}


- (BOOL)rg_isBlank {

    if (self == nil || self == NULL) {
        return YES;
    }

    if ([self isKindOfClass:[NSNull class]]) {
        return YES;
    }

    if ([self isEqualToString:@""]) {
        return YES;
    }

    if ([[self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] == 0) {
        return YES;
    }

    if ([self isEqualToString:@"请选择"]) {
        return YES;
    }

    if ([self isEqualToString:@"必填"]) {
        return YES;
    }

    if ([self isEqualToString:@"请填写"]) {
        return YES;
    }

    return NO;
}

@end
